package com.tamartech.planning.uploader;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;

public class FTPUploader
{
    private String server;
    private String username;
    private String password;
    private String localPath;
    private String localName;
    private String remotePath;
    private String remoteName;
    
    public void run() throws Exception
    {
        FTPClient ftp = new FTPClient();

        FTPClientConfig config = new FTPClientConfig();

        ftp.configure(config);
        boolean error = false;
        try
        {
            int reply;
            ftp.connect (server);

            // After connection attempt, you should check the reply code to
            // verify
            // success.
            reply = ftp.getReplyCode();

            if (!FTPReply.isPositiveCompletion(reply))
                throw new Exception ("FTP server refused connection. FTP Reply string: " + ftp.getReplyString());
            
            if (!ftp.login(username, password))
                throw new Exception ("FTP server failed login.");

            if (!ftp.setFileType(FTP.ASCII_FILE_TYPE))
                throw new Exception ("FTP server failed to set file type.");
            
            if (!ftp.changeWorkingDirectory(remotePath))
                throw new Exception ("FTP server failed to change working directory to: " + remotePath);
            
            InputStream localStream = new FileInputStream (new File (localPath, localName));
            
            if (!ftp.storeFile(remoteName, localStream))
                throw new Exception ("FTP server failed to transfer file to: " + remoteName);
            
            // transfer files
            ftp.logout();
        }
        finally
        {
            if (ftp.isConnected())
            {
                try
                {
                    ftp.disconnect();
                }
                catch (IOException ioe)
                {
                    // do nothing
                }
            }
        }

    }

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub

    }

    public String getServer()
    {
        return server;
    }

    public void setServer(String server)
    {
        this.server = server;
    }

    public String getLocalPath()
    {
        return localPath;
    }

    public void setLocalPath(String localPath)
    {
        this.localPath = localPath;
    }

    public String getLocalName()
    {
        return localName;
    }

    public void setLocalName(String localName)
    {
        this.localName = localName;
    }

    public String getRemotePath()
    {
        return remotePath;
    }

    public void setRemotePath(String remotePath)
    {
        this.remotePath = remotePath;
    }

    public String getRemoteName()
    {
        return remoteName;
    }

    public void setRemoteName(String remoteName)
    {
        this.remoteName = remoteName;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

}
